package com.kuaimacode.kframework.admin.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.user.UserAccountDetailService;
import com.kuaimacode.kframework.admin.services.user.UserService;
import com.kuaimacode.kframework.admin.vo.response.UserAccountDetailVO;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.user.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.admin.controller.user
 * @Description: created by IntelliJ IDEA
 * @date 2019-05-25 21:40
 */
@Controller
@RequestMapping("/userAccount")
public class UserAccountDetailController {

    private static String PAGE_FILE_NAME = "userWithdrawal/";

    @Autowired
    private UserService userService;
    @Autowired
    private UserAccountDetailService accountDetailService;

    @GetMapping("userBalance")
    public String userBalance(
            @RequestParam(value = "pageNum", defaultValue = PageConstants.PAGE_NUM + "") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = PageConstants.PAGE_SIZE + "") int pageSize,
            User searchUser, ModelMap model) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(searchUser.getUsername())) {
            wrapper.like("username", searchUser.getUsername());
        }
        if (StringUtils.isNotBlank(searchUser.getPhone())) {
            wrapper.like("phone", searchUser.getPhone());
        }
        PageHelper.startPage(pageNum, pageSize);
        model.put("pageInfo", PageInfo.of(userService.list(wrapper)));
        model.put("searchUser", searchUser);
        return PAGE_FILE_NAME + "userBalance";
    }

    @GetMapping("index/{userId}")
    public String index(
            @RequestParam(value = "pageNum", defaultValue = PageConstants.PAGE_NUM + "") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = PageConstants.PAGE_SIZE + "") int pageSize,
            @PathVariable("userId") Long userId, UserAccountDetailVO searchVO, ModelMap model) {
        searchVO.setUserId(userId);
        model.put("pageInfo", accountDetailService.findUserBalanceDetailList(searchVO, pageNum, pageSize));
        model.put("searchVO", searchVO);
        return PAGE_FILE_NAME + "accountDetail";
    }
}
